<?php
/**
 * 产品在线订单生成文件
 *
 * @author		Arthur(ArthurXF@gmail.com)
 * @copyright	(c) 2006 by bizeway.com
 * @version		$Id$
 * @package		ArthurXF
 * @subpackage	deal
 */

require_once('config/config.inc.php');
require_once("class/deal.class.php");

$objWebInit = new deal();

//数据库连接参数
$objWebInit->setDBG($arrGPdoDB);
$objWebInit->db();

$user_id = $_SESSION['user_id'];
$user_name = $_SESSION['user_name'];

//订单编号
if (empty($user_id)) {
	$deal_no = date('YmdHis').'_x'.mt_rand(0,100000);
}else{
	$deal_no = date('YmdHis').'_'.$user_id;
}

//用户名
if (empty($user_name)) {
	$user_name = '直接购买';
}

$arrDeal = array();
foreach($_SESSION['cart'] as $k=>$v){
	$arrDeal['deal_no'] = $deal_no;
	$arrDeal['user_id'] = $user_id;
	$arrDeal['user_name'] = $user_name;
	$arrDeal['product'][$k]['product_id'] = $v['id'];
	$arrDeal['product'][$k]['product_title'] = $v['title'];
	$arrDeal['product'][$k]['product_model'] = $v['model'];
	$arrDeal['product'][$k]['product_brand'] = $v['brand'];
	$arrDeal['product'][$k]['product_price'] = $v['my_price'];
	$arrDeal['product'][$k]['product_count'] = $v['count'];
	$arrDeal['deal_price'] += $v['deal_price'];	
}

//产品种类
$arrDeal['product_kind'] = count($_SESSION['cart']);

//如果没登陆，则让填写用户个人信息
if(empty($user_id)){
	if($_SERVER["REQUEST_METHOD"] == "POST"){		
		$arrDeal['contactor'] = $_POST['contactor'];
		$arrDeal['email'] = $_POST['email'];
		$arrDeal['tel'] = $_POST['tel'];
		$arrDeal['mobile'] = $_POST['mobile'];
		$arrDeal['address'] = $_POST['address']; 
		$arrDeal['postcode'] = $_POST['postcode'];
		$arrDeal['company'] = $_POST['company'];
	}	
}

if ($_SERVER["REQUEST_METHOD"] == "POST"){
	
	if(empty($_POST['contactor'])) check::AlertExit('对不起，联系人必须填写!',-1);
	if(empty($_POST['email'])) check::AlertExit('对不起，电子邮件必须填写!',-1);
	if(!check::CheckEmailAddr($_POST['email'])) {
		check::AlertExit('电子邮件格式不合法!',-1);
	}
	
	if(empty($_POST['tel'])) check::AlertExit('对不起，联系电话必须填写!',-1);
	if(empty($_POST['mobile'])) check::AlertExit('对不起，联系手机必须填写!',-1);
	if(empty($_POST['postcode']))	check::AlertExit('对不起，邮编必须填写!!',-1);
	if(empty($_POST['address'])) check::AlertExit('对不起，地址必须填写!',-1);
	if(empty($_POST['company']))	check::AlertExit('对不起，公司名称必须填写!!',-1);

}

$objWebInit->saveInfo($arrDeal,0);
unset($_SESSION['cart']);//确认订单后把session购买记录清空
echo "<script>window.close();</script>";

$arrMOutput["smarty_assign"]['arrUserInfo'] = $arrUserInfo;
$arrMOutput["smarty_assign"]['MAIN'] = $arrGSmarty['admin_main_dir'].'index.htm';
?>